package com.ocl.www.controller;

import com.ocl.www.domain.LyUser;
import com.ocl.www.service.LyUserService;
import io.swagger.models.Model;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.annotation.AdviceName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/10.
 */
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private LyUserService lyUserService;


    @RequestMapping(value = "/aboutMe", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ModelAndView aboutMe(){
        return new ModelAndView("lanyou/about_us");
    }

    @RequestMapping(value = "/getPersonInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> getPersonInfo(){
        Map<String,Object> map = new HashedMap();
        LyUser lyUser = new LyUser();
        lyUser.setUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try {
            lyUser = lyUserService.getUserInfo(lyUser);
            map.put("status","s");
            map.put("info",lyUser);
        } catch (Exception e) {
            map.put("status","e");
            map.put("info",e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Map<String,Object> updateUsr(@RequestBody Map<String,Object> map){
//        Map<String,Object> map = new HashedMap();
        LyUser lyUser = new LyUser();
        lyUser.setUserId(SecurityUtils.getSubject().getPrincipal().toString());
        lyUser.setUserName(map.get("username").toString());
        lyUser.setUserImg(map.get("userimg").toString());
        try {
            int c = lyUserService.updateUser(lyUser);
            if(c==1){
                map.put("status","s");
                map.put("info","成功");
            }else{
                map.put("status","e");
                map.put("info","更新失败");
            }
        } catch (Exception e) {
            map.put("status","e");
            map.put("info",e.getMessage());
        }
        return map;
    }
}
